﻿/**********************************天使物流*******************************************
* 功能描述:系统设置——职位管理
* 作者:孟帅龙
* 时间:2022/1/8
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.System.Positions;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.System.Positions
{
    public class PositionRepository : DapperDbContextRepository, IPositionRepository
    {
        public PositionRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 职位管理——查询分页显示
        /// <summary>
        /// 职位管理——查询分页显示
        /// </summary>
        /// <param name="DeptName"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<PositionDto>> GetPositionPageShow(string PositionName, int pageindex = 1, int pagesize = 3)
        {
            PageResponse<PositionDto> response = new PageResponse<PositionDto>();
            var sql = "select a.Id,a.PositionNumber,a.PositionName,a.Alias,a.DeptId,a.CreateTime,a.IsDelete,b.DeptName from Position a join Department b on a.DeptId=b.Id where a.IsDelete=0";
            if (!string.IsNullOrWhiteSpace(PositionName))
            {
                sql += $" and a.PositionName like '%" + PositionName + "%'";
            }
            var list = await this.QueryAsync<PositionDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<PositionDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 职位管理——逻辑删除
        /// <summary>
        /// 职位管理——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PageResponse<PositionDto>> DeleteDepartment(int id)
        {
            PageResponse<PositionDto> response = new PageResponse<PositionDto>();
            var sql = $"update Position set IsDelete=1 where Id={id}";
            response.Code = await ExecuteAsync(sql, null);
            return response;
        }
        #endregion

        #region 职位管理——批量删除
        /// <summary>
        /// 职位管理——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<PageResponse<PositionDto>> DeleteBatchDepartment(string ids)
        {
            PageResponse<PositionDto> response = new PageResponse<PositionDto>();
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Position set IsDelete=1 where Id={item}";
                response.Code = await ExecuteAsync(sql, null);
            }
            return response;
        }
        #endregion

        #region 职位管理——编辑
        /// <summary>
        /// 职位管理——编辑
        /// </summary>
        /// <param name="departmentDto"></param>
        /// <returns></returns>
        public async Task<int> PostUpdatePosition(PositionDto positionDto)
        {
            PageResponse<PositionDto> response = new PageResponse<PositionDto>();
            var sql = $"update Position set PositionNumber='{positionDto.PositionNumber}',PositionName='{positionDto.PositionName}',Alias='{positionDto.Alias}',DeptId='{positionDto.DeptId}',CreateTime=getdate(),IsDelete=0 where Id='{positionDto.Id}'";
            return await ExecuteAsync(sql, positionDto);
        }
        #endregion

        #region 职位管理——添加
        /// <summary>
        /// 职位管理——添加
        /// </summary>
        /// <param name="rolesDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddPosition(PositionDto positionDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into Position values('{id}','{positionDto.PositionName}','{positionDto.Alias}','{positionDto.DeptId}',getdate(),0)";
            return await ExecuteAsync(sql, positionDto);
        }
        #endregion

        #region 职位下拉框
        /// <summary>
        /// 职位下拉框
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<PositionDto>> GetPosition()
        {
            PageResponse<PositionDto> response = new PageResponse<PositionDto>();
            var sql = $"select * from Position where IsDelete=0";
            response.PageData = await this.QueryAsync<PositionDto>(sql, null);
            return response;
        }
        #endregion
    }
}
